package practiceProjects;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @Project ：learn_java
 * @File ：TimeProportionCalculation.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/5/3 09:51
 * @Description ：计算输入时间占总时间比例
 */


public class TimeProportionCalculation {

    /**
     * 检查输入格式
     * @param i
     * @return
     */
    static boolean checkInput(String i) {
        String[] s = i.split(" ");
        if (s.length > 3) {
            System.out.println("excess spaces");
            return false;
        }

        List<String> list = Arrays.asList(s);
        boolean allShorterThan3 = list.stream().allMatch(j -> j.length() < 3);
        if (!allShorterThan3) {
            System.out.println("longer than 3");
            return false;
        }

        for (char c : i.toCharArray()) {
            if (c != ' ' && !Character.isDigit(c)) {
                System.out.println("contains non numeric character");
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入总时间(空格隔开时分秒)：");
        String totalTime = sc.nextLine();
        if (!checkInput(totalTime)) {
            System.out.println("输入时间格式有误！");
            return;
        }
        System.out.print("输入当前时间(空格隔开时分秒)：");
        String currentTime = sc.nextLine();
        if (!checkInput(currentTime)) {
            System.out.println("输入时间格式有误！");
            return;
        }

        String[] s = totalTime.split(" ");
        // 总时间的时分秒
        int tH = Integer.parseInt(s[0]);
        int tM = Integer.parseInt(s[1]);
        int tS = Integer.parseInt(s[2]);

        String[] s1 = currentTime.split(" ");
        int cH = Integer.parseInt(s1[0]);
        int cM = Integer.parseInt(s1[1]);
        int cS = Integer.parseInt(s1[2]);

        double res = (double) (cH * 3600 + cM * 60 + cS) / (tH * 3600 + tM * 60 + tS);
        System.out.println("占比：" + res);
    }
}
